package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CarUsedListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CarUsed;
import com.isunland.managesystem.entity.CarUsedOriginal;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/6 15:29
 */
public class CarUsedApplyLisFragment extends BaseListFragment {
  private ArrayList<CarUsed> mlist;
  private int REQUEST_DETAIL = 0;
  private int REQUEST_QUERY = 1;
  private String mBeginDate;
  private String mEndDate;
  private String mCarNumber;
  private CarUsedListAdapter madapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.carUseApply);
    setIsPaging(true);//分页
  }

  @Override public void initData() {
    mlist = new ArrayList<>();
    mBeginDate = MyDateUtil.parseDateToString(MyDateUtil.getBeforeMonth(1));
    mEndDate = MyDateUtil.parseDateToString(MyDateUtil.getAfterMonth(1));
    mCarNumber = "";
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_add_search, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_add:
        //添加
        Intent add = new Intent(getActivity(), CarUseApplyActivity.class);
        startActivityForResult(add, REQUEST_DETAIL);
        return true;
      case R.id.menu_item_querys:
        //查询
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(mBeginDate);
        queryConten.setExtName2(mEndDate);
        queryConten.setExtName3(mCarNumber);
        Intent query = new Intent(getActivity(), CarUsedApplyQueryActivity.class);
        query.putExtra(CarUsedApplyQueryFragment.EXTRA_CONTENT, queryConten);
        startActivityForResult(query, REQUEST_QUERY);

        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_CAR_USED_APPLY_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("beginuseDate", mBeginDate);
    params.put("enduseDate", mEndDate);
    params.put("licensePlateNumber", mCarNumber);
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    CarUsedOriginal original = new Gson().fromJson(json, CarUsedOriginal.class);
    if (Base.SUCCESS == original.getResult() && original.getRows() != null) {
      if (getCurrentPage() == 1) {
        mlist.clear();
      }
      mlist.addAll(original.getRows());
      if (madapter == null) {
        madapter = new CarUsedListAdapter(getActivity(), mlist, CarUsedListAdapter.CAR_APPLY);
        setListAdapter(madapter);
      } else {
        madapter.notifyDataSetChanged();
      }
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    CarUsed content = mlist.get(position - 1);
    Intent intent = new Intent(getActivity(), CarUseApplyActivity.class);
    intent.putExtra(CarUseApplyFragment.EXTRA_CONTENT, content);
    startActivityForResult(intent, REQUEST_DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_QUERY) {
      ExtraValueContent queryConten =
          (ExtraValueContent) data.getSerializableExtra(CarUsedApplyQueryFragment.EXTRA_CONTENT);
      mBeginDate = queryConten.getExtName1();
      mEndDate = queryConten.getExtName2();
      mCarNumber = queryConten.getExtName3();
    }

    refreshFromTop();//从顶部刷新
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}
